import {HTTP} from "../utils/http";
class ClassicModel extends HTTP{
    getLatest(callback){
        this.request({
            url:"/classic/latest",
            success:res=>{
                let index=res.index
                callback(res);
                wx.setStorageSync('latest', res.index)
            }
        })
    }
//     getPrevious(index,callback){
//     this.request({
//         url:`/classic/${index}/previous`,
//         success:res=>{
//             callback(res);
//         }
//     })
// }
// getNext(index,callback){
//     this.request({
//         url:`/classic/${index}/next`,
//         success:res=>{
//             callback(res);
//         }
//     })
// }

//简化代码，封装
getClassic(index,nextOrprevious,callback){
    let inx=(nextOrprevious=="next")?index+1:index-1;
    const classic=wx.getStorageSync(this._getKey(inx));
    if(classic){
        callback(classic)
    }else{
        this.request({
        url:`/classic/${index}/${nextOrprevious}`,
        success:res=>{
            callback(res);
            wx.setStorageSync(this._getKey(res.index),res)
        }
    })
    }
}
_getKey(index){
  return "classic"+index;
}
isFirst(index){
    return index==1?true:false
}
isLatest(index){
    const latest=wx.getStorageSync('latest');
    return index==latest?true:false;
}
getFavor(callback){
    this.request({
     url:"/classic/favor",
     success:res=>{
         callback(res)
     }
    })
}
}
export {ClassicModel}